// 显示用户列表
$(document).ready(function () {
    let showUsers = function () {
        $.ajax({
            type: 'get',
            url: '/admin/usersShow',
            data: '',
            success: function (data) {
                let usersHtml = template('users_template', { list: data });
                $('tboby').html(usersHtml);
            }
        })
    }
})
showUsers();

// 添加用户
$('#add-user')
    .bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            user_num: {
                message: 'The username is not valid',
                validators: {
                    notEmpty: {
                        message: '账号必须填写'
                    },
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: '账号长度在6到30个字符之间'
                    },
                    regexp: {
                        // regexp: /^[a-zA-Z0-9_\.]+$/,
                        regexp: /^[1-9]\d{4,10}$/,
                        message: '您的账号的格式有误'
                    }
                }
            },
            user_password: {
                validators: {
                    notEmpty: {
                        message: '密码不能为空'
                    },
                    stringLength: {
                        min: 1,
                        max: 30,
                        message: '密码长度在6到30个字符之间'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: '密码只能由数字字母下划线组成'
                    }
                }
            },
            user_name: {
                validators: {
                    notEmpty: {
                        message: '用户名必须填写'
                    }
                }
            }
        }
    })
    .on('success.form.bv', function (e) {
        // Prevent form submission
        e.preventDefault();

        // Get the form instance
        var $form = $(e.target);

        // Get the BootstrapValidator instance
        var bv = $form.data('bootstrapValidator');
        // Use Ajax to submit form data
        console.log($form.serialize());
        $.ajax({
            type: 'post',
            url: '/admin/userAdd',
            data: $form.serialize(),
            success: function (data) {
                // console.log(data);
                if (data.code == '1001') {
                    showUsers();
                    location.reload();
                }
            }
        })
    });